package com.net.shop.car.manager.location;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

public class LocationService implements LocationListener {

	/**
	 * 经度
	 */
	private double longitude;
	/**
	 * 维度
	 */
	private double laitude;
	/**
	 * 时间
	 */
	private long time;
	
	/**
	 * 海拔
	 */
	private double altitude;
	
	private static LocationService ls;
	
	private LocationManager lm;
	/**
	 * 
	 */
	private Activity context;

	/**
	 * @return the longitude
	 */
	public double getLongitude() {
		return longitude;
	}

	/**
	 * @return the laitude
	 */
	public double getLaitude() {
		return laitude;
	}

	/**
	 * @return the time
	 */
	public long getTime() {
		return time;
	}

	/**
	 * @return the altitude
	 */
	public double getAltitude() {
		return altitude;
	}

	private LocationService() {

	}

	public static LocationService ls() {
		if (ls == null) {
			ls = new LocationService();
		}
		return ls;
	}

	/**
	 * @param context
	 *            Context
	 */
	public void init(Activity context) {
		lm = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
		if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Toast.makeText(context, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
			// 返回开启GPS导航设置界面
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			context.startActivityForResult(intent, 0);
			return;
		}
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
		
		
	}
	
	public void dettach() {
		lm.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		longitude = location.getLongitude();
		laitude = location.getLatitude();
		time = location.getTime();
		System.out.println("longitude: " + longitude + ";laitude" + laitude);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		switch (status) {
		// GPS状态为可见时
		case LocationProvider.AVAILABLE:
//			Log.i(TAG, "当前GPS状态为可见状态");
//			Toast.makeText(context, "当前GPS状态为可见状态", Toast.LENGTH_SHORT).show();
			break;
		// GPS状态为服务区外时
		case LocationProvider.OUT_OF_SERVICE:
//			Log.i(TAG, "当前GPS状态为服务区外状态");
//			Toast.makeText(context, "当前GPS状态为服务区外状态", Toast.LENGTH_SHORT).show();
			break;
		// GPS状态为暂停服务时
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
//			Log.i(TAG, "当前GPS状态为暂停服务状态");
//			Toast.makeText(context, "当前GPS状态为暂停服务状态", Toast.LENGTH_SHORT).show();
			break;
		}
	}

	@Override
	public void onProviderEnabled(String provider) {
		Location location=lm.getLastKnownLocation(provider);
		longitude = location.getLongitude();
		laitude = location.getLatitude();
		time = location.getTime();
		System.out.println("longitude: " + longitude + ";laitude" + laitude);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}
}
